package com.authine.cloudpivot.ext.dao;

import com.authine.cloudpivot.ext.utils.FinancialCalculationUtils;
import com.authine.mvp.app.launcher.domain.AttachmentFormat;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import javax.persistence.Column;
import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName QuotationGoods
 * @Description: 报价商品
 * @Author fj
 * @Date 2021/4/28
 * @Version V1.0
 **/
@Data
@Schema(description = "报价商品")
public class QuotationGoods extends SystemFields{
    /**
     * 会员id
     */
    @Column(name = "member_id")
    private List<SelectorFormat> member_id;

    /**
     * 询价商品id
     */
    @Column(name = "inquiry_goods_id")
    private String inquiry_goods_id;

    /**
     * 组织类目名称
     */
    @Column(name = "org_category_name")
    private String org_category_name;

    /**
     * 状态
     */
    @Column(name = "quotation_goods_status")
    private String quotation_goods_status;

    /**
     * 组织SKU号
     */
    private String sku;

    /**
     * 条形码
     */
    @Column(name = "bar_code")
    private String bar_code;

    /**
     * 商品名
     */
    private String name;

    /**
     * 品牌
     */
    private String brand;

    /**
     * 型号
     */
    private String model;

    /**
     * 规格
     */
    private String spec;

    /**
     * 单价
     */
    private BigDecimal price;

    /**
     * 税前单价
     */
    @Column(name = "pretax_price")
    private BigDecimal pretax_price;

    /**
     * 货期（-1为现货）
     */
    private BigDecimal delivery;

    /**
     * 数量
     */
    private BigDecimal num;

    /**
     * 单位
     */
    private String unit;

    /**
     * 包装数量
     */
    @Column(name = "package_num")
    private BigDecimal package_num;

    /**
     * 包装单价
     */
    @Column(name = "package_unit")
    private String package_unit;

    /**
     * 税率
     */
    @Column(name = "tax_rate")
    private String tax_rate;

    /**
     * 税额
     */
    @Column(name = "tax_payable")
    private BigDecimal tax_payable;

    /**
     * 税后运费
     */
    private BigDecimal freight;

    /**
     * 运费税率
     */
    @Column(name = "tax_rate_freight")
    private String tax_rate_freight;

    /**
     * 税前运费
     */
    @Column(name = "pretax_freight")
    private BigDecimal pretax_freight;

    /**
     * 运费税额
     */
    @Column(name = "tax_payable_freight")
    private BigDecimal tax_payable_freight;

    /**
     * 单价*数量
     */
    private BigDecimal total;

    /**
     * 单价*数量
     */
    @Column(name = "pretax_total")
    private BigDecimal pretax_total;

    /**
     * 小计
     */
    private BigDecimal amount;

    /**
     * 税前小计
     */
    @Column(name = "pretax_amount")
    private BigDecimal pretax_amount;

    /**
     * 过期时间
     */
    private String expire;

    /**
     * 发货地
     */
    private String address;

    /**
     * 是否定标
     */
    private Boolean picketage;

    /**
     * 描述
     */
    private String description;

    /**
     * 操作人id
     */
    private List<SelectorFormat> operator;

    /**
     * 外部来源client_id
     */
    @Column(name = "client_id")
    private String client_id;

    /**
     * 外部来源报价商品id
     */
    @Column(name = "client_quotation_goods_id")
    private String client_quotation_goods_id;

    /**
     * 定标时对应该询价商品中所有的报价商品的平均单价
     */
    @Column(name = "average_price")
    private BigDecimal average_price;

    /**
     * 定标理由
     */
    @Column(name = "picketage_reason")
    private String picketage_reason;

    /**
     * 审批任务状态
     */
    @Column(name = "task_ver")
    private BigDecimal task_ver;

    /**
     * 审批意见
     */
    @Column(name = "approval_opinion")
    private String approval_opinion;

    /**
     * 当前审批人
     */
    @Column(name = "approval_by")
    private List<SelectorFormat> approval_by;

    /**
     * 平台虚拟供应商操作人d
     */
    @Column(name = "platform_quotation_id")
    private List<SelectorFormat> platform_quotation_id;

    /**
     * 是否为自动报价
     */
    @Column(name = "auto_quotation")
    private Boolean auto_quotation;

    /**
     * 供应商部门名称
     */
    @Column(name = "vendor_department_name")
    private String vendor_department_name;

    /**
     * 供应商部门id
     */
    @Column(name = "vendor_department_id")
    private List<SelectorFormat> vendor_department_id;

    /**
     * member_mobile
     */
    @Column(name = "member_mobile")
    private String member_mobile;

    /**
     * member_email
     */
    @Column(name = "member_email")
    private String member_email;

    /**
     * member_full_name
     */
    @Column(name = "member_full_name")
    private String member_full_name;

    /**
     * vendor_organization_id
     */
    @Column(name = "vendor_organization_id")
    private List<SelectorFormat> vendor_organization_id;

    /**
     * vendor_organization_description
     */
    @Column(name = "organization_description")
    private String organization_description;

    /**
     * vendor_organization_name
     */
    @Column(name = "vendor_organization_name")
    private String vendor_organization_name;

    /**
     * platform_quotation_name
     */
    @Column(name = "platform_quotation_name")
    private String platform_quotation_name;

    /**
     * user_name
     */
    @Column(name = "user_name")
    private String user_name;

    /**
     * 最大订货量
     */
    private BigDecimal maximum;

    /**
     * 供应商备注(标记信息)
     */
    @Column(name = "remark_message")
    private String remark_message;

    /**
     * 最小起订量
     */
    @Column(name = "min_order")
    private BigDecimal min_order;

    /**
     * 是否为虚拟供应商
     */
    @Column(name = "virtual_vendor")
    private Boolean virtual_vendor;

    /**
     * 外部来源供应商商品id
     */
    @Column(name = "client_vendor_goods_id")
    private String client_vendor_goods_id;

    /**
     * 报价来源（普通报价、自动报价、平台报价）
     */
    private String source;

    /**
     * 询价单标题
     */
    @Column(name = "inquiry_name")
    private String inquiry_name;

    /**
     * 询价人
     */
    @Column(name = "member_name")
    private String member_name;

    /**
     * 澄清状态
     */
    @Column(name = "clarify_status")
    private String clarify_status;

    /**
     * 询价单号
     */
    @Column(name = "inquiry_id")
    private String inquiry_id;

    /**
     * 询价商品状态
     */
    @Column(name = "inquiry_goods_status")
    private String inquiry_goods_status;

    /**
     * 分类
     */
    @Column(name = "platform_category_name")
    private String platform_category_name;

    /**
     * 报价次数
     */
    @Column(name = "quote_count")
    private BigDecimal quote_count;

    /**
     * 参与次数
     */
    @Column(name = "partake_count")
    private BigDecimal partake_count;

    /**
     * 询价截止日期
     */
    @Column(name = "inquiry_expire_time")
    private String inquiry_expire_time;

    /**
     * 询价发布日期
     */
    @Column(name = "inquiry_time")
    private String inquiry_time;

    /**
     * 客户组织名称
     */
    @Column(name = "organization_company_name")
    private String organization_company_name;

    /**
     * 还价原因
     */
    @Column(name = "approved_failed_reason")
    private String approved_failed_reason;

    /**
     * 还价价格(未税)
     */
    @Column(name = "counter_pre_price")
    private BigDecimal counter_pre_price;

    /**
     * 还价截止日期
     */
    @Column(name = "counter_price_date")
    private String counter_price_date;

    /**
     * 询价商品组织sku号
     */
    @Column(name = "inquiry_sku")
    private String inquiry_sku;

    /**
     * 附件
     */
    private List<AttachmentFormat> attachment;

    /**
     * 澄清内容
     */
    @Column(name = "clarify_tips")
    private String clarify_tips;

    /**
     * 澄清附件上传
     */
    @Column(name = "clarify_attachment")
    private List<AttachmentFormat> clarify_attachment;

    /**
     * 用户id
     */
    @Column(name = "user_id")
    private String user_id;

    /**
     * 通过税前单价计算金额
     * @param pretax_price
     */
    public void setPretaxPrice(BigDecimal pretax_price) {
        BigDecimal taxRate = new BigDecimal(tax_rate);
        BigDecimal taxRateFreight = new BigDecimal(tax_rate_freight);
        if (null != pretax_price) {
            if (null != tax_rate) {
                this.price = FinancialCalculationUtils.calculatePriceByPretaxPrice(pretax_price, taxRate);
            }
            if (null != num) {
                this.pretax_total = FinancialCalculationUtils.calculatePretaxTotalByPretaxPrice(pretax_price, num);
                if (null != tax_rate) {
                    this.total = FinancialCalculationUtils.calculateTotalByPretaxPrice(pretax_price, taxRate, num);
                    if (null != pretax_freight && null != tax_rate && null != tax_rate_freight) {
                        this.amount = FinancialCalculationUtils.calculateAmountByPretaxPriceAndPretaxFreight(pretax_price, pretax_freight, taxRate, taxRateFreight, num);
                    }
                    this.tax_payable = FinancialCalculationUtils.calculateTaxPayableByPretaxPrice(pretax_price, taxRate, num);
                }
                if (null != pretax_freight) {
                    this.pretax_amount = FinancialCalculationUtils.calculatePretaxAmountByPretaxPriceAndPretaxFreight(pretax_price, pretax_freight, num);
                }
            }

        }
        if (null != pretax_freight && null != tax_rate_freight) {
            this.freight = FinancialCalculationUtils.calculateFreightByPretaxFreight(pretax_freight, taxRateFreight);
            this.tax_payable_freight = FinancialCalculationUtils.calculatePayableFreightByPretaxFreight(pretax_freight, taxRateFreight);
        }
        this.pretax_price = pretax_price;
    }
}
